﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Demo.Common;
using Demo.Lib;

namespace Demo.Impl {
    public abstract class BaseStrategy<T> : IConversionMappingStrategy {

        private Dictionary<char, T> mapping = new Dictionary<char, T>();


        public BaseStrategy() {
            this.Init();
        }

        protected void Init() {
            using (var reader = File.OpenText("words.txt")) {
                reader.ReadLine();
                foreach (var line in reader.ReadAllLines()) {
                    var tokens = line.Split('\t');
                    var key = this.GetKey(tokens);
                    var value = this.GetValue(tokens);
                    mapping[key] = value;
                }
            }
        }

        public abstract char GetKey(string[] tokens);

        public abstract T GetValue(string[] tokens);

        public virtual IEnumerable<string> Convert(System.IO.TextReader reader) {
            StringBuilder buffer = new StringBuilder();
            foreach (var line in reader.ReadAllLines()) {
                buffer.Clear();
                foreach (var c in line) {
                    if (mapping.ContainsKey(c)) {
                        buffer.Append(mapping[c]);
                    }
                    else {
                        buffer.Append(c);
                    }
                }
                yield return buffer.ToString();
            }
        }
    }
}
